remove dependency to telegram client; migrate the according code to httmultiparty

Tom König 8 years ago
parent
commit
c54b68ddff
4 changed files with 43 additions and 64 deletions
  1. 12 12
      Gemfile
  2. 9 31
      Gemfile.lock
  3. 16 15
      app/models/agents/telegram_agent.rb
  4. 6 6
      spec/models/agents/telegram_agent_spec.rb

+ 12 - 12
Gemfile

@@ -25,18 +25,17 @@ end
25 25
 
26 26
 # Optional libraries.  To conserve RAM, comment out any that you don't need,
27 27
 # then run `bundle` and commit the updated Gemfile and Gemfile.lock.
28
-gem 'twilio-ruby', '~> 3.11.5'       # TwilioAgent
29
-gem 'ruby-growl', '~> 4.1.0'         # GrowlAgent
30
-gem 'net-ftp-list', '~> 3.2.8'       # FtpsiteAgent
31
-gem 'wunderground', '~> 1.2.0'       # WeatherAgent
32
-gem 'forecast_io', '~> 2.0.0'        # WeatherAgent
33
-gem 'rturk', '~> 2.12.1'             # HumanTaskAgent
34
-gem 'hipchat', '~> 1.2.0'            # HipchatAgent
35
-gem 'xmpp4r',  '~> 0.5.6'            # JabberAgent
36
-gem 'mqtt'                           # MQTTAgent
37
-gem 'slack-notifier', '~> 1.0.0'     # SlackAgent
38
-gem 'hypdf', '~> 1.0.7'              # PDFInfoAgent
39
-gem 'telegram-bot-ruby', '~> 0.4.1'  # TelegramAgent
28
+gem 'twilio-ruby', '~> 3.11.5'    # TwilioAgent
29
+gem 'ruby-growl', '~> 4.1.0'      # GrowlAgent
30
+gem 'net-ftp-list', '~> 3.2.8'    # FtpsiteAgent
31
+gem 'wunderground', '~> 1.2.0'    # WeatherAgent
32
+gem 'forecast_io', '~> 2.0.0'     # WeatherAgent
33
+gem 'rturk', '~> 2.12.1'          # HumanTaskAgent
34
+gem 'hipchat', '~> 1.2.0'         # HipchatAgent
35
+gem 'xmpp4r',  '~> 0.5.6'         # JabberAgent
36
+gem 'mqtt'                        # MQTTAgent
37
+gem 'slack-notifier', '~> 1.0.0'  # SlackAgent
38
+gem 'hypdf', '~> 1.0.10'          # PDFInfoAgent
40 39
 
41 40
 # Weibo Agents
42 41
 gem 'weibo_2', github: 'cantino/weibo_2', branch: 'master'
@@ -101,6 +100,7 @@ gem 'foreman', '~> 0.63.0'
101 100
 gem 'geokit', '~> 1.8.4'
102 101
 gem 'geokit-rails', '~> 2.0.1'
103 102
 gem 'httparty', '~> 0.13'
103
+gem 'httmultiparty', '~> 0.3.16'
104 104
 gem 'jquery-rails', '~> 3.1.3'
105 105
 gem 'json', '~> 1.8.1'
106 106
 gem 'jsonpath', '~> 0.5.6'

+ 9 - 31
Gemfile.lock

@@ -112,10 +112,6 @@ GEM
112 112
       multi_json (>= 1.0.0)
113 113
     aws-sdk-core (2.2.15)
114 114
       jmespath (~> 1.0)
115
-    axiom-types (0.1.1)
116
-      descendants_tracker (~> 0.0.4)
117
-      ice_nine (~> 0.11.0)
118
-      thread_safe (~> 0.3, >= 0.3.1)
119 115
     bcrypt (3.1.10)
120 116
     better_errors (1.1.0)
121 117
       coderay (>= 1.0.0)
@@ -150,8 +146,6 @@ GEM
150 146
     chronic (0.10.2)
151 147
     cliver (0.3.2)
152 148
     coderay (1.1.0)
153
-    coercible (1.0.0)
154
-      descendants_tracker (~> 0.0.1)
155 149
     coffee-rails (4.1.1)
156 150
       coffee-script (>= 2.2.0)
157 151
       railties (>= 4.0.0, < 5.1.x)
@@ -176,8 +170,6 @@ GEM
176 170
       activesupport (>= 3.0, < 5.0)
177 171
     delorean (2.1.0)
178 172
       chronic
179
-    descendants_tracker (0.0.4)
180
-      thread_safe (~> 0.3, >= 0.3.1)
181 173
     devise (3.5.4)
182 174
       bcrypt (~> 3.0)
183 175
       orm_adapter (~> 0.1)
@@ -233,8 +225,6 @@ GEM
233 225
       dotenv (>= 0.7)
234 226
       thor (>= 0.13.6)
235 227
     formatador (0.2.5)
236
-    gene_pool (1.4.1)
237
-      thread_safe
238 228
     geokit (1.8.5)
239 229
       multi_json (>= 1.3.2)
240 230
     geokit-rails (2.0.1)
@@ -277,21 +267,22 @@ GEM
277 267
     hipchat (1.2.0)
278 268
       httparty
279 269
     hpricot (0.8.6)
280
-    httmultiparty (0.3.10)
270
+    httmultiparty (0.3.16)
281 271
       httparty (>= 0.7.3)
272
+      mimemagic
282 273
       multipart-post
283 274
     http (0.6.4)
284 275
       http_parser.rb (~> 0.6.0)
285 276
     http-cookie (1.0.2)
286 277
       domain_name (~> 0.5)
287 278
     http_parser.rb (0.6.0)
288
-    httparty (0.13.1)
279
+    httparty (0.13.7)
289 280
       json (~> 1.8)
290 281
       multi_xml (>= 0.5.2)
291
-    hypdf (1.0.7)
292
-      httmultiparty (= 0.3.10)
282
+    hypdf (1.0.10)
283
+      httmultiparty (~> 0.3)
284
+      httparty (~> 0.13)
293 285
     i18n (0.7.0)
294
-    ice_nine (0.11.2)
295 286
     jmespath (1.1.3)
296 287
     jquery-rails (3.1.3)
297 288
       railties (>= 3.0, < 5.0)
@@ -329,6 +320,7 @@ GEM
329 320
       thread_safe (~> 0.3, >= 0.3.1)
330 321
     method_source (0.8.2)
331 322
     mime-types (2.99.1)
323
+    mimemagic (0.3.1)
332 324
     mini_magick (4.2.3)
333 325
     mini_portile2 (2.0.0)
334 326
     minitest (5.8.4)
@@ -382,11 +374,6 @@ GEM
382 374
       multi_json (~> 1.3)
383 375
       omniauth-oauth (~> 1.0)
384 376
     orm_adapter (0.5.0)
385
-    persistent_http (1.0.6)
386
-      gene_pool (>= 1.3)
387
-    persistent_httparty (0.1.2)
388
-      httparty (~> 0.9)
389
-      persistent_http (< 2)
390 377
     pg (0.18.3)
391 378
     poltergeist (1.8.1)
392 379
       capybara (~> 2.1)
@@ -531,10 +518,6 @@ GEM
531 518
       net-ssh (>= 2.8.0)
532 519
     string-scrub (0.0.5)
533 520
     systemu (2.6.4)
534
-    telegram-bot-ruby (0.4.1)
535
-      httmultiparty
536
-      persistent_httparty
537
-      virtus
538 521
     term-ansicolor (1.3.0)
539 522
       tins (~> 1.0)
540 523
     therubyracer (0.12.2)
@@ -579,11 +562,6 @@ GEM
579 562
       macaddr (~> 1.0)
580 563
     uuidtools (2.1.5)
581 564
     vcr (2.9.2)
582
-    virtus (1.0.5)
583
-      axiom-types (~> 0.1)
584
-      coercible (~> 1.0)
585
-      descendants_tracker (~> 0.0, >= 0.0.3)
586
-      equalizer (~> 0.0, >= 0.0.9)
587 565
     warden (1.2.4)
588 566
       rack (>= 1.0)
589 567
     webmock (1.17.4)
@@ -641,8 +619,9 @@ DEPENDENCIES
641 619
   guard-rspec (~> 4.6.4)
642 620
   haversine
643 621
   hipchat (~> 1.2.0)
622
+  httmultiparty (~> 0.3.16)
644 623
   httparty (~> 0.13)
645
-  hypdf (~> 1.0.7)
624
+  hypdf (~> 1.0.10)
646 625
   jquery-rails (~> 3.1.3)
647 626
   json (~> 1.8.1)
648 627
   jsonpath (~> 0.5.6)
@@ -689,7 +668,6 @@ DEPENDENCIES
689 668
   spring (~> 1.6.3)
690 669
   spring-commands-rspec (~> 1.0.4)
691 670
   string-scrub
692
-  telegram-bot-ruby (~> 0.4.1)
693 671
   therubyracer (~> 0.12.2)
694 672
   tumblr_client!
695 673
   twilio-ruby (~> 3.11.5)

+ 16 - 15
app/models/agents/telegram_agent.rb

@@ -1,18 +1,17 @@
1
-require 'telegram/bot'
1
+require 'httmultiparty'
2 2
 require 'open-uri'
3 3
 require 'tempfile'
4 4
 
5 5
 module Agents
6 6
   class TelegramAgent < Agent
7
+    include HTTMultiParty
8
+    base_uri 'https://api.telegram.org/'
9
+
7 10
     cannot_be_scheduled!
8 11
     cannot_create_events!
9 12
     no_bulk_receive!
10 13
 
11
-    gem_dependency_check { defined?(Telegram) }
12
-
13 14
     description <<-MD
14
-      #{'# Include `telegram-bot-ruby` in your Gemfile to use this Agent!' if dependencies_missing?}
15
-
16 15
       The Telegram Agent receives and collects events and sends them via [Telegram](https://telegram.org/).
17 16
 
18 17
       It is assumed that events have either a `text`, `photo`, `audio`, `document` or `video` key. You can use the EventFormattingAgent if your event does not provide these keys.
@@ -51,16 +50,20 @@ module Agents
51 50
 
52 51
     private
53 52
 
54
-    TELEGRAM_FIELDS = {
55
-      text:     :send_message,
56
-      photo:    :send_photo,
57
-      audio:    :send_audio,
58
-      document: :send_document,
59
-      video:    :send_video
53
+    TELEGRAM_ACTIONS = {
54
+      text:     :sendMessage,
55
+      photo:    :sendPhoto,
56
+      audio:    :sendAudio,
57
+      document: :sendDocument,
58
+      video:    :sendVideo
60 59
     }.freeze
61 60
 
61
+    def telegram_bot_uri(method)
62
+      "/bot#{interpolated['auth_token']}/#{method}"
63
+    end
64
+
62 65
     def receive_event(event)
63
-      TELEGRAM_FIELDS.each do |field, method|
66
+      TELEGRAM_ACTIONS.each do |field, method|
64 67
         payload = load_field event, field
65 68
         next unless payload
66 69
         send_telegram_message method, field => payload
@@ -69,9 +72,7 @@ module Agents
69 72
 
70 73
     def send_telegram_message(method, params)
71 74
       params[:chat_id] = interpolated['chat_id']
72
-      Telegram::Bot::Client.run interpolated['auth_token'] do |bot|
73
-        bot.api.send method, params
74
-      end
75
+      TelegramAgent.post telegram_bot_uri(method), query: params
75 76
     end
76 77
 
77 78
     def load_field(event, field)

+ 6 - 6
spec/models/agents/telegram_agent_spec.rb

@@ -55,8 +55,8 @@ describe Agents::TelegramAgent do
55 55
       @checker.receive [event_0, event_1]
56 56
 
57 57
       expect(@sent_messages).to eq([
58
-        { send_message: { text: 'Looks like its going to rain' } },
59
-        { send_message: { text: 'Another text message' } }
58
+        { sendMessage: { text: 'Looks like its going to rain' } },
59
+        { sendMessage: { text: 'Another text message' } }
60 60
       ])
61 61
     end
62 62
 
@@ -64,28 +64,28 @@ describe Agents::TelegramAgent do
64 64
       event = event_with_payload photo: 'https://example.com/image.png'
65 65
       @checker.receive [event]
66 66
 
67
-      expect(@sent_messages).to eq([{ send_photo: { photo: :stubbed_file } }])
67
+      expect(@sent_messages).to eq([{ sendPhoto: { photo: :stubbed_file } }])
68 68
     end
69 69
 
70 70
     it 'accepts audio key and uses :send_audio to send the file' do
71 71
       event = event_with_payload audio: 'https://example.com/sound.mp3'
72 72
       @checker.receive [event]
73 73
 
74
-      expect(@sent_messages).to eq([{ send_audio: { audio: :stubbed_file } }])
74
+      expect(@sent_messages).to eq([{ sendAudio: { audio: :stubbed_file } }])
75 75
     end
76 76
 
77 77
     it 'accepts document key and uses :send_document to send the file' do
78 78
       event = event_with_payload document: 'https://example.com/document.pdf'
79 79
       @checker.receive [event]
80 80
 
81
-      expect(@sent_messages).to eq([{ send_document: { document: :stubbed_file } }])
81
+      expect(@sent_messages).to eq([{ sendDocument: { document: :stubbed_file } }])
82 82
     end
83 83
 
84 84
     it 'accepts video key and uses :send_video to send the file' do
85 85
       event = event_with_payload video: 'https://example.com/video.avi'
86 86
       @checker.receive [event]
87 87
 
88
-      expect(@sent_messages).to eq([{ send_video: { video: :stubbed_file } }])
88
+      expect(@sent_messages).to eq([{ sendVideo: { video: :stubbed_file } }])
89 89
     end
90 90
   end
91 91